package com.jeeplus.itemInterface.accountInterface;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.modules.account.entity.Account;
import com.jeeplus.modules.account.entity.BalanceInfo;
import com.jeeplus.modules.account.service.AccountService;
import com.jeeplus.modules.centerrecord.entity.CenterRecord;
import com.jeeplus.modules.centerrecord.service.CenterRecordService;
import com.jeeplus.modules.dynamicrecord.entity.DynamicRecord;
import com.jeeplus.modules.dynamicrecord.service.DynamicRecordService;
import com.jeeplus.modules.freezingassets.entity.FreezingAssets;
import com.jeeplus.modules.freezingassets.service.FreezingAssetsService;
import com.jeeplus.modules.givebalance.entity.GiveBalance;
import com.jeeplus.modules.givebalance.service.GiveBalanceService;
import com.jeeplus.modules.integraldetail.entity.IntegralDetail;
import com.jeeplus.modules.integraldetail.service.IntegralDetailService;
import com.jeeplus.modules.member.entity.Member;
import com.jeeplus.modules.member.service.MemberService;
import com.jeeplus.modules.orderbalance.service.OrderBalanceService;
import com.jeeplus.modules.orders.service.OrdersService;
import com.jeeplus.modules.positionassets.entity.PositionAssets;
import com.jeeplus.modules.positionassets.service.PositionAssetsService;
import com.jeeplus.modules.purchaseorder.entity.PurchaseOrder;
import com.jeeplus.modules.purchaseorder.service.PurchaseOrderService;
import com.jeeplus.modules.recharge.entity.BalanceDetail;
import com.jeeplus.modules.recharge.entity.Recharge;
import com.jeeplus.modules.recharge.service.RechargeService;
import com.jeeplus.modules.subrecord.entity.SubRecord;
import com.jeeplus.modules.subrecord.service.SubRecordService;
import com.jeeplus.modules.sys.service.SystemService;
import com.jeeplus.modules.teamrecord.entity.TeamInfo;
import com.jeeplus.modules.teamrecord.entity.TeamRecord;
import com.jeeplus.modules.teamrecord.service.TeamRecordService;
import com.jeeplus.modules.teamservice.entity.TeamService;
import com.jeeplus.modules.teamservice.service.TeamServiceService;
import com.jeeplus.modules.tixian.entity.Tixian;
import com.jeeplus.modules.tixian.service.TixianService;
import com.jeeplus.modules.traderecord.entity.TradeRecord;
import com.jeeplus.modules.traderecord.service.TradeRecordService;
import com.jeeplus.modules.yuyue.service.YuyueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Api(value = "AccountInterfaceController", description = "账户信息")
@Controller
@RequestMapping(value = "${frontPath}/account")
public class AccountInterfaceController {

	@Autowired
	private MemberService memberService;
	@Autowired
	private AccountService accountService;
	@Autowired
	private DynamicRecordService dynamicRecordService;
	@Autowired
	private TixianService tixianService;
	@Autowired
	private RechargeService rechargeService;
	@Autowired
	private GiveBalanceService giveBalanceService;
	@Autowired
	private SubRecordService subRecordService;
	@Autowired
	private TradeRecordService tradeRecordService;
	@Autowired
	private PositionAssetsService positionAssetsService;
	@Autowired
	private FreezingAssetsService freezingAssetsService;
	@Autowired
	private TeamServiceService teamServiceService;
	@Autowired
	private IntegralDetailService integralDetailService;
	@Autowired
	private CenterRecordService centerRecordService;
	@Autowired
	private PurchaseOrderService purchaseOrderService;
	@Autowired
	private TeamRecordService teamRecordService;


	
	
	@ResponseBody
	@GetMapping("getMemberAccount")
	@ApiOperation(notes = "获取用户账户信息", value = "获取用户账户信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "memberid", value = "用户id", dataType = "String", paramType = "query", required = true) })
	public AjaxJson getMemberAccount(HttpServletRequest request, HttpServletResponse response) {
		AjaxJson aj = new AjaxJson();
		String memberid = request.getParameter("memberid");
		Member member = memberService.get(memberid);
		if (member != null) {
			Account account = accountService.get(member.getAccount().getId());
			aj.put("accout", account);
			aj.setSuccess(true);
		} else {
			aj.setSuccess(false);
			aj.setMsg("找不到用户");
		}
		return aj;
	}


	@ResponseBody
	@GetMapping("getBalanceDetail")
	@ApiOperation(notes = "余额明细", value = "余额明细")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "memberid", value = "用户id", dataType = "String", paramType = "query", required = true) })
	public AjaxJson getBalanceDetail(HttpServletRequest request, HttpServletResponse response) {
		AjaxJson aj = new AjaxJson();
		List<BalanceDetail> balanceDetails = new ArrayList<>();
		String memberid = request.getParameter("memberid");
		Member member = memberService.get(memberid);
		if (member != null) {
			Account account = accountService.get(member.getAccount().getId());
			//提现记录
			Tixian tixian = new Tixian();
			tixian.setMember(member);
			List<Tixian> tixianList = tixianService.findTodayList(tixian);
			if (tixianList.size()>0){
				for (Tixian tixian1 : tixianList) {
					BalanceDetail balanceDetail = new BalanceDetail();
					balanceDetail.setChangeDate(DateUtils.formatDateTime(tixian1.getUpdateDate()));
					balanceDetail.setChangeMoney(tixian1.getMoney());
					balanceDetail.setChangeFlag("0");
					balanceDetails.add(balanceDetail);
				}
			}
			//充值成功的记录
			Recharge recharge = new Recharge();
			recharge.setMemberId(memberid);
			List<Recharge> rechargeList = rechargeService.findTodayList(recharge);
			if (rechargeList.size()>0){
				for (Recharge recharge1 : rechargeList) {
					BalanceDetail balanceDetail = new BalanceDetail();
					balanceDetail.setChangeDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(recharge1.getUpdateDate()));
					balanceDetail.setChangeMoney(recharge1.getRechargeMoney());

					balanceDetail.setChangeFlag("1");
					balanceDetails.add(balanceDetail);
				}
			}
			//买入
			TradeRecord purchaseRecord = new TradeRecord();
			purchaseRecord.setPurchaseMember(member);
			List<TradeRecord> tradeRecordList = tradeRecordService.findDaysPurchaseInfo(purchaseRecord);
			if (tradeRecordList.size()>0){
				for (TradeRecord tradeRecord1 : tradeRecordList) {
					BalanceDetail balanceDetail = new BalanceDetail();
					balanceDetail.setChangeDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(tradeRecord1.getCreateDate()));
					balanceDetail.setChangeMoney(tradeRecord1.getTotalPrice());
					balanceDetail.setChangeFlag("2");
					balanceDetails.add(balanceDetail);
				}
			}
			//卖出
			TradeRecord saleRecord = new TradeRecord();
			saleRecord.setSaleMember(member);
			List<TradeRecord> tradeRecordList1 = tradeRecordService.findDaysSaleInfo(saleRecord);
			if (tradeRecordList1.size()>0){
				for (TradeRecord tradeRecord1 : tradeRecordList1) {
					BalanceDetail balanceDetail = new BalanceDetail();
					balanceDetail.setChangeDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(tradeRecord1.getCreateDate()));
					balanceDetail.setChangeMoney(tradeRecord1.getTotalPrice());
					balanceDetail.setChangeFlag("3");
					balanceDetails.add(balanceDetail);
				}
			}
			// 认购收益 制度
//			Double subscribe = 0.0;
			DynamicRecord dynamicRecord = new DynamicRecord();
			dynamicRecord.setMember(member);
			List<DynamicRecord> list = dynamicRecordService.findListByDays(dynamicRecord);
			if (list.size()>0){
				for (DynamicRecord record : list) {
					BalanceDetail balanceDetail = new BalanceDetail();
					balanceDetail.setChangeDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(record.getCreateDate()));
					balanceDetail.setChangeMoney(record.getMoney());
					balanceDetail.setChangeFlag("4");
					balanceDetails.add(balanceDetail);
				}
//				subscribe = list.stream().mapToDouble(DynamicRecord::getMoney).sum();

			}
			//交易收益
			TradeRecord purchaseRecord1 = new TradeRecord();
			purchaseRecord1.setPurchaseMember(member);
			List<TradeRecord> tradeRecordList2 = tradeRecordService.findDaysPurchaseInfo(purchaseRecord1);
			TradeRecord saleRecord1 = new TradeRecord();
			saleRecord1.setSaleMember(member);
			List<TradeRecord> tradeRecordList3 = tradeRecordService.findDaysSaleInfo(saleRecord1);
			if (tradeRecordList2.size()>0){
				for (TradeRecord tradeRecord1 : tradeRecordList2) {
					if (tradeRecordList3.size()>0){
						for (TradeRecord tradeRecord : tradeRecordList3) {
							if (tradeRecord.getCreateDate() == tradeRecord1.getCreateDate()){
								BalanceDetail balanceDetail = new BalanceDetail();
								balanceDetail.setChangeDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(tradeRecord1.getCreateDate()));
								balanceDetail.setChangeMoney(tradeRecord.getTotalPrice()-tradeRecord1.getTotalPrice());
								balanceDetail.setChangeFlag("5");
								balanceDetails.add(balanceDetail);
							}
						}
					}
				}
			}
			//认购商品
			SubRecord subRecord = new SubRecord();
			subRecord.setMember(member);
			List<SubRecord> subRecordList = subRecordService.findListByDays(subRecord);
			if (subRecordList.size()>0){
				for (SubRecord record : subRecordList) {
					BalanceDetail balanceDetail = new BalanceDetail();
					balanceDetail.setChangeDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(record.getCreateDate()));
					balanceDetail.setChangeMoney(record.getTotalPrice());
					balanceDetail.setChangeFlag("6");
					balanceDetails.add(balanceDetail);
				}
			}
			//服务中心
			CenterRecord centerRecord = new CenterRecord();
			centerRecord.setMember(member);
			List<CenterRecord> records = centerRecordService.findListByDays(centerRecord);
			if (subRecordList.size()>0){
				for (CenterRecord record : records) {
					BalanceDetail balanceDetail = new BalanceDetail();
					balanceDetail.setChangeDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(record.getCreateDate()));
					balanceDetail.setChangeMoney(record.getMoney());
					balanceDetail.setChangeFlag("7");
					balanceDetails.add(balanceDetail);
				}
			}
			List<BalanceDetail> listSort = balanceDetails.stream().sorted(Comparator.comparing(BalanceDetail::getChangeDate).reversed()).collect(Collectors.toList());
			aj.put("balance", account.getYue());
			aj.put("balanceDetail",listSort);
			aj.setSuccess(true);
		} else {
			aj.setSuccess(false);
			aj.setMsg("找不到用户");
		}
		return aj;
	}


    @ResponseBody
    @GetMapping("getTeamDetail")
    @ApiOperation(notes = "团队明细", value = "团队明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "memberid", value = "用户id", dataType = "String", paramType = "query", required = true) })
    public AjaxJson getTeamDetail(HttpServletRequest request, HttpServletResponse response) {
        AjaxJson aj = new AjaxJson();
        String memberid = request.getParameter("memberid");
        Member member = memberService.get(memberid);
        if (member != null) {
        	Double dynamic = 0.0;
        	Double service = 0.0;
			Double integral = 0.0;
            //  团队业绩  团队收益
			Account account = accountService.get(member.getAccount().getId());
			Member member1 = new Member();
			member1.setP(member);
			List<Member> memberList = memberService.findList(member1);
			//查询团队下级人员
			List<Member>  queryList = new ArrayList<>();
			List<Member> list = new ArrayList<>();
			queryList.addAll(memberList);
			list.addAll(memberList);
			while (list.size()>0){
				list.clear();
				List<Member> list1 = new ArrayList<>();
				for (Member member2 : queryList) {
					Member entity = new Member();
					entity.setP(member2);
					List<Member> memberList1 = memberService.findList(entity);
					memberList.addAll(memberList1);
					list1.addAll(memberList1);
				}
				list.addAll(list1);
				queryList.clear();
				queryList.addAll(list1);
			}
			//认购收益
			DynamicRecord dynamicRecord = new DynamicRecord();
			dynamicRecord.setMember(member);
			List<DynamicRecord> records = dynamicRecordService.findList(dynamicRecord);
			if (records.size()>0){
				dynamic = records.stream().mapToDouble(DynamicRecord::getMoney).sum();
			}

			//手续费
			TeamService teamService = new TeamService();
			teamService.setMember(member);
			List<TeamService> serviceList = teamServiceService.findList(teamService);
			if (serviceList.size()>0){
				service = serviceList.stream().mapToDouble(TeamService::getMoney).sum();
			}
			//积分收益
			TeamRecord teamRecord = new TeamRecord();
			teamRecord.setMember(member);
			List<TeamRecord> teamRecords = teamRecordService.findList(teamRecord);
			if (teamRecords.size() > 0){
				integral = teamRecords.stream().mapToDouble(TeamRecord::getMoney).sum();
			}
			aj.setSuccess(true);
			//总团队人数
            aj.put("teamNum",account.getNum());
            //总团队业绩
            aj.put("team",BigDecimal.valueOf(account.getTeam()).setScale(2,BigDecimal.ROUND_HALF_DOWN));
            //团队收益
			aj.put("teamProfit",BigDecimal.valueOf(account.getTeamProfit()).setScale(2,BigDecimal.ROUND_HALF_DOWN));
			//认购收益
			aj.put("dynamic",BigDecimal.valueOf(dynamic).setScale(2,BigDecimal.ROUND_HALF_DOWN));
			//手续费收益
			aj.put("service",BigDecimal.valueOf(service).setScale(2,BigDecimal.ROUND_HALF_DOWN));
			//积分收益
			aj.put("integral",BigDecimal.valueOf(integral).setScale(2,BigDecimal.ROUND_HALF_DOWN));
        } else {
            aj.setSuccess(false);
            aj.setMsg("找不到用户");
        }
        return aj;
    }

//	@ResponseBody
//	@GetMapping("getTEST")
//	@ApiOperation(notes = "测试接口", value = "测试接口")
//	public AjaxJson getTEST(HttpServletRequest request, HttpServletResponse response) {
//		AjaxJson aj = new AjaxJson();
//		List<Member> list2 = memberService.findList(new Member());
//		for (Member member : list2) {
//			Double money=0.0;
//			SubRecord subRecord=new SubRecord();
//			subRecord.setMember(member);
//			List<SubRecord> list3 = subRecordService.findList(subRecord);
//			Member member1 = new Member();
//			member1.setP(member);
//			List<Member> memberList = memberService.findList(member1);
//			//查询团队下级人员
//			List<Member>  queryList = new ArrayList<>();
//			List<Member> list = new ArrayList<>();
//			queryList.addAll(memberList);
//			list.addAll(memberList);
//			while (list.size()>0){
//				list.clear();
//				List<Member> list1 = new ArrayList<>();
//				for (Member member2 : queryList) {
//					Member entity = new Member();
//					entity.setP(member2);
//					List<Member> memberList1 = memberService.findList(entity);
//					memberList.addAll(memberList1);
//					list1.addAll(memberList1);
//				}
//				list.addAll(list1);
//				queryList.clear();
//				queryList.addAll(list1);
//			}
//			for (Member member2 : memberList) {
//				SubRecord subRecord1=new SubRecord();
//				subRecord1.setMember(member2);
//				List<SubRecord> list1 = subRecordService.findList(subRecord1);
//				list3.addAll(list1);
//			}
//			for (SubRecord record : list3) {
//				money=money+record.getTotalPrice();
//			}
//			Account account = accountService.get(member.getAccount().getId());
//			account.setTeam(money);
//			account.setNum(memberList.size());
//			account.setTodayProfit(account.getTodayDynamic()+account.getTodayTeam());
//			accountService.save(account);
//		}
////		List<Account> list = accountService.findList(new Account());
////		for (Account account : list) {
////			account.setTodayProfit(account.getTodayDynamic()+account.getTodayTeam());
////			accountService.save(account);
////		}
//		return aj;
//	}


	@ResponseBody
	@GetMapping("getInviteDetail")
	@ApiOperation(notes = "直推明细", value = "直推明细")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "memberid", value = "用户id", dataType = "String", paramType = "query", required = true) })
	public AjaxJson getInviteDetail(HttpServletRequest request, HttpServletResponse response) {
		AjaxJson aj = new AjaxJson();
		String memberid = request.getParameter("memberid");
		Member member = memberService.get(memberid);
		if (member != null) {
			Member entity = new Member();
			entity.setP(member);
			List<Member> list = memberService.findList(entity);
			if (list.size()>0){
				for (Member member1 : list) {
					Double m=0.0;
					SubRecord subRecord=new SubRecord();
					subRecord.setMember(member1);
					List<SubRecord> list1 = subRecordService.findList(subRecord);
					if (list1.size()>0){
						for (SubRecord record : list1) {
							m=m+record.getTotalPrice();
						}
					}
					member1.setSubMoney(m);
					if (memberService.isOrNotEffective(member1)){
						member1.setIsEffective("1");
					}else {
						member1.setIsEffective("0");
					}
				}
			}
			aj.setSuccess(true);
			aj.put("inviteList",list);
		} else {
			aj.setSuccess(false);
			aj.setMsg("找不到用户");
		}
		return aj;
	}


	@ResponseBody
	@GetMapping("getTeamProfiteDetail")
	@ApiOperation(notes = "团队收益明细", value = "团队收益明细")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "memberid", value = "用户id", dataType = "String", paramType = "query", required = true) })
	public AjaxJson getTeamProfiteDetail(HttpServletRequest request, HttpServletResponse response) {
		AjaxJson aj = new AjaxJson();
		String memberid = request.getParameter("memberid");
		Member member = memberService.get(memberid);
		if (member != null) {
			List<BalanceDetail> balanceDetails = new ArrayList<>();
			DynamicRecord dynamicRecord = new DynamicRecord();
			dynamicRecord.setMember(member);
			List<DynamicRecord> dynamicRecords = dynamicRecordService.findListByDays(dynamicRecord);
			if (dynamicRecords.size()>0){
				for (DynamicRecord record : dynamicRecords) {
					BalanceDetail balanceDetail = new BalanceDetail();
					balanceDetail.setChangeMoney(record.getMoney());
					balanceDetail.setChangeDate(DateUtils.formatDateTime(record.getCreateDate()));
					balanceDetail.setChangeFlag("认购收益");
					balanceDetails.add(balanceDetail);
				}
			}
			TeamService teamService = new TeamService();
			teamService.setMember(member);
			List<TeamService> teamList = teamServiceService.findListByDays(teamService);
			if (teamList.size()>0){
				for (TeamService record : teamList) {
					BalanceDetail balanceDetail = new BalanceDetail();
					balanceDetail.setChangeMoney(record.getMoney());
					balanceDetail.setChangeDate(DateUtils.formatDateTime(record.getCreateDate()));
					balanceDetail.setChangeFlag("手续费收益");
					balanceDetails.add(balanceDetail);
				}
			}
			List<BalanceDetail> listSort = balanceDetails.stream().sorted(Comparator.comparing(BalanceDetail::getChangeDate).reversed()).collect(Collectors.toList());
			aj.put("list",listSort);
			aj.setSuccess(true);
		} else {
			aj.setSuccess(false);
			aj.setMsg("找不到用户");
		}
		return aj;
	}


	@ResponseBody
	@GetMapping("getPositionDetail")
	@ApiOperation(notes = "持仓资产明细", value = "持仓资产明细")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "memberid", value = "用户id", dataType = "String", paramType = "query", required = true) })
	public AjaxJson getPositionDetail(HttpServletRequest request, HttpServletResponse response) {
		AjaxJson aj = new AjaxJson();
		String memberid = request.getParameter("memberid");
		Member member = memberService.get(memberid);
		if (member != null) {
			//持仓
			Double position = 0.0;
			List<PositionAssets> list = new ArrayList<>();
			//持仓资产
			PositionAssets positionAssets = new PositionAssets();
			positionAssets.setMember(member);
			List<PositionAssets> positionAssetsList = positionAssetsService.findDaysList(positionAssets);
			if (positionAssetsList.size()>0){
				position = positionAssetsList.stream().mapToDouble(PositionAssets::getNowTotal).sum();
				list.addAll(positionAssetsList);
			}
			aj.put("position",position);
			aj.put("list",list);
		} else {
			aj.setSuccess(false);
			aj.setMsg("找不到用户");
		}
		return aj;
	}




	@ResponseBody
	@GetMapping("getFreezingDetail")
	@ApiOperation(notes = "下架产品", value = "下架产品")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "memberid", value = "用户id", dataType = "String", paramType = "query", required = true) })
	public AjaxJson getFreezingDetail(HttpServletRequest request, HttpServletResponse response) {
		AjaxJson aj = new AjaxJson();
		String memberid = request.getParameter("memberid");
		Member member = memberService.get(memberid);
		if (member != null) {
			//锁仓
			Double freezing = 0.0;
			List<FreezingAssets> list = new ArrayList<>();
			//锁仓资产
			FreezingAssets freezingAssets = new FreezingAssets();
			freezingAssets.setMember(member);
			List<FreezingAssets> freezingAssetsList = freezingAssetsService.findDaysList(freezingAssets);
			if (freezingAssetsList.size()>0){
				freezing = freezingAssetsList.stream().mapToDouble(FreezingAssets::getNowTotal).sum();
				list.addAll(freezingAssetsList);
			}
			aj.put("freezing",freezing);
			aj.put("list",list);
		} else {
			aj.setSuccess(false);
			aj.setMsg("找不到用户");
		}
		return aj;
	}


	@ResponseBody
	@PostMapping("giveBalance")
	@ApiOperation(notes = "余额互转", value = "余额互转")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "memberid", value = "用户id", dataType = "String", paramType = "query", required = true),
			@ApiImplicitParam(name = "toMember", value = "转交用户手机号", dataType = "String", paramType = "query", required = true),
			@ApiImplicitParam(name = "money", value = "转交金额", dataType = "String", paramType = "query", required = true)
	})
	public AjaxJson giveBalance(HttpServletRequest request, HttpServletResponse response) {
		AjaxJson aj = new AjaxJson();
		String memberid = request.getParameter("memberid");
		String toMember = request.getParameter("toMember");
		String money = request.getParameter("money");
		Member member = memberService.get(memberid);
		if (member != null) {
			Member tomem = memberService.getMemberByLoginName(toMember);
			if (tomem == null){
				aj.setSuccess(false);
				aj.setMsg("转交用户不存在");
				return aj;
			}
			if (isStr2Num(money)){
				BigDecimal m = BigDecimal.valueOf(Double.valueOf(money));
				if (m.compareTo(BigDecimal.valueOf(100)) == 0 || m.compareTo(BigDecimal.valueOf(100)) == 1 && Double.valueOf(money) % 100 == 0){
					Account account = accountService.get(member.getAccount().getId());
					if (m.compareTo(BigDecimal.valueOf(account.getYue())) == 1){
						aj.setSuccess(false);
						aj.setMsg("余额不足");
						return aj;
					}
					//余额互转
					accountService.giveBalance(member,tomem,m);
				}else {
					aj.setSuccess(false);
					aj.setMsg("转交金额为100的倍数");
					return aj;
				}
			}else {
				aj.setSuccess(false);
				aj.setMsg("请输入正确的数字");
				return aj;
			}
			aj.setSuccess(true);
			aj.setMsg("转交成功");
		} else {
			aj.setSuccess(false);
			aj.setMsg("找不到用户");
		}
		return aj;
	}


	/**
	 * 查看一个字符串是否可以转换为数字
	 * @param str 字符串
	 * @return true 可以; false 不可以
	 */
	public static boolean isStr2Num(String str) {
		try {
			Double.parseDouble(str);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}


	@ResponseBody
	@GetMapping("getIntegralDetail")
	@ApiOperation(notes = "积分明细", value = "积分明细")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "memberid", value = "用户id", dataType = "String", paramType = "query", required = true) })
	public AjaxJson getIntegralDetail(HttpServletRequest request, HttpServletResponse response) {
		AjaxJson aj = new AjaxJson();
		String memberid = request.getParameter("memberid");
		Member member = memberService.get(memberid);
		if (member != null) {
			IntegralDetail integralDetail = new IntegralDetail();
			integralDetail.setMember(member);
			List<IntegralDetail> integralDetailList = integralDetailService.findListByDays(integralDetail);

			integralDetailList = integralDetailList.stream().sorted(Comparator.comparing(IntegralDetail::getCreateDate).reversed()).collect(Collectors.toList());
			aj.put("list",integralDetailList);
			aj.setSuccess(true);
		} else {
			aj.setSuccess(false);
			aj.setMsg("找不到用户");
		}
		return aj;
	}


	@ResponseBody
	@GetMapping("getAssetsInfo")
	@ApiOperation(notes = "账户资产概况", value = "账户资产概况")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "memberid", value = "用户id", dataType = "String", paramType = "query", required = true)
	})
	public AjaxJson getAssetsInfo(HttpServletRequest request, HttpServletResponse response) {
		AjaxJson aj = new AjaxJson();
		String memberid = request.getParameter("memberid");
		Member member = memberService.get(memberid);
		if (member != null) {
			//认购收益 制度
			Double subscribe = 0.0;
			DynamicRecord dynamicRecord = new DynamicRecord();
			dynamicRecord.setMember(member);
			List<DynamicRecord> list = dynamicRecordService.findListByDays(dynamicRecord);
			if (list.size()>0){
				subscribe = list.stream().mapToDouble(DynamicRecord::getMoney).sum();
			}
			//持仓盈亏
			Double upPosition = 0.0;
			PositionAssets positionAssets = new PositionAssets();
			positionAssets.setMember(member);
			positionAssets.setIsApply("0");
			List<PositionAssets> positionAssetsList = positionAssetsService.findList(positionAssets);
			if (positionAssetsList.size()>0){
				for (PositionAssets assets : positionAssetsList) {
					TradeRecord tradeRecord = new TradeRecord();
					tradeRecord.setSubGoods(assets.getSubGoods());
					List<TradeRecord> tradeRecords = tradeRecordService.findList(tradeRecord);
					if (tradeRecords.size()>0){
						subscribe += (tradeRecords.get(0).getPrice()-assets.getGoodsPrice())*assets.getGoodsNum();
					}
				}
			}
			//今日盈亏
			Double todayUp = 0.0;
			TradeRecord tradeRecord = new TradeRecord();
			tradeRecord.setSaleMember(member);
			List<TradeRecord> saleList = tradeRecordService.findTodayInfo(tradeRecord);
			if (saleList.size()>0){
//				for (TradeRecord record : saleList) {
//					record.setTotalPrice(record.getPrice()*record.getNum());
//				}
				todayUp += saleList.stream().mapToDouble(TradeRecord::getTotalPrice).sum();
			}
			TradeRecord tradeRecord1 = new TradeRecord();
			tradeRecord1.setPurchaseMember(member);
			List<TradeRecord> purchaseList = tradeRecordService.findTodayInfo(tradeRecord1);
			if (purchaseList.size()>0){
//				for (TradeRecord record : purchaseList) {
//					record.setTotalPrice(record.getPrice()*record.getNum());
//				}
				todayUp -= purchaseList.stream().mapToDouble(TradeRecord::getTotalPrice).sum();
			}
			aj.put("subscribe",subscribe);
			aj.put("upPosition",upPosition);
			aj.put("todayUp",todayUp);
			aj.setSuccess(true);
		} else {
			aj.setSuccess(false);
			aj.setMsg("找不到用户");
		}
		return aj;
	}


	@ResponseBody
	@GetMapping("getServiceDetail")
	@ApiOperation(notes = "服务中心明细", value = "服务中心明细")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "memberid", value = "用户id", dataType = "String", paramType = "query", required = true) })
	public AjaxJson getServiceDetail(HttpServletRequest request, HttpServletResponse response) {
		AjaxJson aj = new AjaxJson();
		String memberid = request.getParameter("memberid");
		Member member = memberService.get(memberid);
		List<CenterRecord> list1 = new ArrayList<>();
		List<CenterRecord> list2 = new ArrayList<>();
		//认购收益
		double sub = 0.0;
		//积分收益
		double integral = 0.0;
		if (member != null) {
			CenterRecord centerRecord = new CenterRecord();
			centerRecord.setMember(member);
			List<CenterRecord> list = centerRecordService.findList(centerRecord);
			if (list.size()>0){
				for (CenterRecord record : list) {
					if ("0".equals(record.getCatchType())){
						list1.add(record);
					}else if ("1".equals(record.getCatchType())){
						list2.add(record);
					}
				}
				sub = list1.stream().mapToDouble(CenterRecord::getMoney).sum();
				integral = list2.stream().mapToDouble(CenterRecord::getMoney).sum();
			}
			aj.put("member",member);
			aj.put("sub",BigDecimal.valueOf(sub).setScale(2,BigDecimal.ROUND_HALF_DOWN));
			aj.put("integral",BigDecimal.valueOf(integral).setScale(2,BigDecimal.ROUND_HALF_DOWN));
			aj.put("list",list);
			aj.setSuccess(true);
		} else {
			aj.setSuccess(false);
			aj.setMsg("找不到用户");
		}
		return aj;
	}


	@ApiOperation(value = "获取用户可提现金额",notes = "获取用户可提现金额")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "用户id",name = "memberId",required = true,dataType = "String",paramType = "query"),
	})
	@GetMapping("/getMemberCash")
	@ResponseBody
	public AjaxJson getMemberCash(String memberId){
		AjaxJson aj = new AjaxJson();
		Member member = memberService.get(memberId);
		if (member != null){
			Account account = accountService.get(member.getAccount().getId());
			if (account != null){
				//查询用户现在的待交易的买入订单
				PurchaseOrder purchaseOrder = new PurchaseOrder();
				purchaseOrder.setMember(member);
				purchaseOrder.setIsRevoke("0");
				purchaseOrder.setIsDeal("0");
				List<PurchaseOrder> list = purchaseOrderService.findList(purchaseOrder);
				double totalMoney = 0.0;
				if (list.size()>0){
					totalMoney = list.stream().mapToDouble(PurchaseOrder::getTotalPrice).sum();
				}
				//总共要买入花费的金额
				double cash = BigDecimal.valueOf(account.getYue()-totalMoney).setScale(2,BigDecimal.ROUND_HALF_DOWN).doubleValue();
				aj.put("cash",cash);
				aj.setSuccess(true);
			}else {
				aj.setSuccess(false);
				aj.setMsg("账户信息有误");
			}
		}else {
			aj.setSuccess(false);
			aj.setMsg("用户信息有误");
		}
		return aj;
	}

}
